import * as monaco from 'monaco-editor';
export const pythonLanguages: monaco.languages.IMonarchLanguage = {
  tokenPostfix: '.python',
  keywords: [
    'and',
    'as',
    'assert',
    'async',
    'await',
    'break',
    'class',
    'continue',
    'def',
    'del',
    'elif',
    'else',
    'except',
    'False',
    'finally',
    'for',
    'from',
    'global',
    'if',
    'import',
    'in',
    'is',
    'lambda',
    'None',
    'nonlocal',
    'not',
    'or',
    'pass',
    'raise',
    'return',
    'True',
    'try',
    'while',
    'with',
    'yield',
  ],
  builtins: [
    'abs',
    'all',
    'any',
    'ascii',
    'bin',
    'bool',
    'bytearray',
    'bytes',
    'callable',
    'chr',
  ],
  operators: [
    '=',
    '>',
    '<',
    '!',
    '~',
    '?',
    ':',
    '==',
    '<=',
    '>=',
    '!=',
    '&&',
    '||',
    '++',
    '--',
    '+',
    '-',
    '*',
    '/',
    '&',
    '|',
    '^',
    '%',
    '<<',
    '>>',
    '>>>',
    '+=',
    '-=',
    '*=',
    '/=',
    '&=',
    '|=',
    '^=',
    '%=',
    '<<=',
    '>>=',
    '>>>=',
  ],
  symbols: /[=><!~?:&|+\-*/^%@]+/,
  escapes: /\\(?:[abfnrtv\\"']|x[\da-fA-F]{1,4}|u[\da-fA-F]{4}|U[\da-fA-F]{8})/,
  tokenizer: {
    root: [
      // 多行字符串 ("""...""") 和 ('...'')
      [/(?:'''[\s\S]*?'''|"""[\s\S]*?""")/, 'string.multiline'],

      // 单引号和双引号字符串
      [/".*?"/, 'string.doublequote'],
      [/'.*?'/, 'string.singlequote'],

      // 关键字、内置函数等
      [
        /[a-zA-Z_]\w*/,
        {
          cases: {
            '@keywords': 'keyword',
            '@builtins': 'predefined',
            '@default': 'identifier',
          },
        },
      ],
      { include: '@whitespace' },
      [/[{}()$$<>]/, '@brackets'], // 添加尖括号匹配
      [
        /@symbols/,
        {
          cases: {
            '->': 'operator', // 处理Python箭头操作符
            '@operators': 'operator',
            '@default': '',
          },
        },
      ],
      [
        /(\b)(def|class)(\s+)(\w+)/,
        ['keyword', 'keyword', 'white', 'type.identifier'],
      ],
      [/\b[0-9_]*\.?[0-9]+([eE][-+]?[0-9]+)?\b/, 'number'],
      [/(\b)(True|False|None)\b/, ['keyword', 'keyword']],
    ],
    whitespace: [
      [/[ \t\r\n]+/, 'white'],
      [/#.*$/, 'comment'],
    ],
    string_double: [
      [/[^\\"]+/, 'string'],
      [/@escapes/, 'string.escape'],
      [/\\./, 'string.escape.invalid'],
      [/"/, 'string', '@pop'],
    ],
    string_single: [
      [/[^\\']+/, 'string'],
      [/@escapes/, 'string.escape'],
      [/\\./, 'string.escape.invalid'],
      [/'/, 'string', '@pop'],
    ],
  },
};
